package com.xw.echo.core.nacos;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class NacosRegisterServer {
    private String serverName;

    @Setter
    @Getter
    private Instance instance=null;
    private NamingService namingService;
    public NacosRegisterServer(NamingService namingService, Instance instance,String serverName){
        this.namingService = namingService;
        this.instance = instance;
        this.serverName=serverName;
    }
    public NacosRegisterServer(NamingService namingService,String serverName){
        this(namingService,null,serverName);
    }
    public void register(Instance instance){
        try {
            namingService.registerInstance(serverName, instance);
        } catch (NacosException e) {
            log.error("Failed to initialize NamingService", e);
        }
    }
    public void  deregister(){
        try {
            namingService.deregisterInstance(serverName, instance);
        } catch (NacosException e) {
            e.printStackTrace();
        }
    }

}
